﻿using System.Collections.ObjectModel;
using System.Data;
using System.Text;
using System.Web;

namespace Chart.Net
{
    public class TableChart : Chart
    {
        private Collection<Column> columns = new Collection<Column>();
        private Collection<Row> rows = new Collection<Row>();
        public Collection<Column> Columns
        {
            get { return columns; }
        }

        /// <summary>
        /// Default constructor for TableEntity, need to add columns before adding rows.
        /// </summary>
        public TableChart()
        {
        }

        /// <summary>
        /// Constructs a TableEntity from a DataTable
        /// </summary>
        /// <param name="dataTable">DataTable entity</param>
        public TableChart(DataTable dataTable)
        {
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                Column column = new Column(dataTable.Columns[i].ColumnName);
                columns.Add(column);
            }
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                if (dataTable.Rows[i].RowState != DataRowState.Deleted)  //added by freistli, Aug.14, 2013
                {
                    Row newRow = NewRow();
                    for (int j = 0; j < columns.Count; j++)
                    {
                        newRow[j].CellValue = (dataTable.Rows[i][j] == null ? null : dataTable.Rows[i][j].ToString());
                    }
                }
            }
        }


        /// <summary>
        /// Creates a new row in the table.
        /// </summary>
        /// <returns>The new row in the table. Null if columns is not added.</returns>
        public Row NewRow()
        {
            if (columns.Count == 0)
            {
                return null;
            }
            Row newRow = new Row(columns.Count);
            rows.Add(newRow);
            return newRow;
        }

        public Collection<Row> Rows
        {
            get { return rows; }
        }

        public override string GetBody()
        {
            StringBuilder result = new StringBuilder();
            result.Append("<div class=\"table-container\">");
            result.AppendFormat("<h1 id=\"{1}\">{0}</h1><div>", Title, string.IsNullOrEmpty(Title) ? "untitled" : Title.Replace(' ', '-'));
            result.Append("<table class=\"display datatable\"><thead><tr>");
            for (int i = 0; i < columns.Count; i++)
            {
                result.AppendFormat("<th>{0}</th>", columns[i].ColumnName);
            }
            result.Append("</tr></thead><tbody>");
            for (int i = 0; i < rows.Count; i++)
            {
                result.Append("<tr class=\"gradeA\">");
                for(int j = 0; j < columns.Count; j++)
                {
                    result.AppendFormat("<td>{0}</td>", HttpUtility.HtmlEncode(rows[i][j].CellValue));
                }
                result.Append("</tr>");
            }
            result.Append("</tbody><tfoot><tr>");
            for (int i = 0; i < columns.Count; i++)
            {
                result.AppendFormat("<th>{0}</th>", columns[i].ColumnName);
            }
            result.Append("</tr></tfoot></table></div></div>");
            return result.ToString();
        }


        public override string GetScripts()
        {
            return "<script src=\"../scripts/jquery.datatable/jquery.dataTables.min.js\"></script>" +
                "<script src=\"../scripts/jquery.datatable/table.js\"></script>";
        }

        #region Inner Class

        public class Column
        {
            public string ColumnName { get; set; }

            public Column(string name)
            {
                ColumnName = name;
            }
        }

        public class Row
        {
            private Cell[] cells;
            public Row(int columnNumber)
            {
                cells = new Cell[columnNumber];
                for (int i = 0; i < columnNumber; i++)
                {
                    cells[i] = new Cell();
                }
            }

            public Cell this[int index]
            {
                get { return cells[index]; }
                set { cells[index] = value; }
            }
        }

        public class Cell
        {
            public string CellValue { get; set; }
        }

        #endregion
    }
}
